package Week.w151;

import org.junit.Test;

import java.util.*;

public class Solution2 {

    public int[] numSmallerByFrequency(String[] queries, String[] words) {
        int[] wordF=new int[words.length];
        for(int i=0;i< words.length;i++){
            wordF[i]=f(words[i]);
        }
        int[] result=new int[queries.length];
        for(int i=0;i<queries.length;i++){
            int qf=f(queries[i]);
            for(int j=0;j<wordF.length;j++){
                if(wordF[j]<qf){
                    result[i]+=1;
                }
            }
        }
        return result;
    }



   private Integer f(String word){
        Map<Character,Integer> map=new HashMap<>();
       PriorityQueue<Character> q = new PriorityQueue<>();
       for(int i=0;i<word.length();i++){
           char c=word.charAt(i);
           if(map.containsKey(c)){
               map.put(c,map.get(c)+1);
           }else {
               map.put(c,1);
               q.add(c);
           }
       }

       return map.get(q.poll());

   }

    @Test
    public void test(){
       String[] queries = new String[]{"cbd"};
       String[]words = new String[]{"zaaaz"};
        numSmallerByFrequency(queries,words);
    }
}
